package com.liming.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.liming.model.CartGoods;
import com.liming.model.Orders;
import com.liming.utils.DataSourceUtils;

public class Ordersdao {

	QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
	/**
	 * 获取userid下所有订单
	 * @param userid
	 * @return
	 */
	public List<Orders> getAllOrder(int userid) {
		
		List<Orders> orders = null;
		
		String sql = "Select * from order where userid = ?";
		try {
			orders = qr.query(sql, new BeanListHandler<>(Orders.class),userid);
			if (orders.size()==0||orders==null) {
				return null;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return orders;
	}
	
	
	/**
	 * 创建订单
	 * @param orders
	 * @return
	 */
	public int createOrder(Orders orders) {
		
		String sql = "Insert into order values(?,?,?,?,?,?)";
		Object []params = {
				orders.getOrdernum(),
				orders.getCreattime(),
				orders.getUserid(),
				orders.getGoodsid(),
				orders.getGoodsnum(),
				orders.getOrderstate()
		};
		
		int ans=0;
		try {
			ans=qr.update(sql,params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(ans);
		return ans;
		
	}

	
	/**
	 * 取消订单
	 * @param userid
	 * @param ordernum
	 * @return
	 */
	public int cancelOrder(int userid, String ordernum) {
		
		//订单 支付 与 未支付 都被删除；
		String sql = "Delete from order where userid = ? and ordernum = ?";
		Object []params = {userid,ordernum};
		
		int ans=0;
		try {
			ans=qr.update(sql,params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ans;
		
	}


	
	public int payOffOrder(int userid, String ordernum,int state) {
		
		return 0;
	}
	
	
	
	
	
}
